1 /* 简单调用实例 */
2
3 uint32_t pulse, period; //定义PWM占空比时间、周期结果变量,单位采用us
4
5 PWM current_pwm;
6
7 //初始化PWM对象,定时器单位时间为1us,最大定时值为0x10000
8 pwm_input_init(¤t_pwm, us_to_ns(1), 0x10000);
9
10 //外部IO中断覆盖调用,基于cubeMX开发
11 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
12 {
13 IO_STATUS flag = IO_STATUS_LOW;
uint32_t curr_cnt; ///********** 此处很重要 ***********/ //如果刚好遇上定时器更新时间,但定时器中断优先级低于外部IO中断,所以,此处做特别处理,防止时间出错,因为定时器计数器是不断更新的 if (__HAL_TIM_GET_FLAG(&htim3, TIM_FLAG_UPDATE) != RESET){ if (__HAL_TIM_GET_IT_SOURCE(&htim3, TIM_IT_UPDATE) != RESET){ current_pwm.pwm_input_timer_count(¤t_pwm); curr_cnt = htim3.Instance->CNT; __HAL_TIM_CLEAR_IT(&htim3, TIM_IT_UPDATE); } }else curr_cnt = htim3.Instance->CNT;
17 if(GPIO_Pin==GPIO_PIN_6){
18 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6)==GPIO_PIN_SET){ //高电平,上升沿
19 flag = IO_STATUS_HIGH;
20 }else flag = IO_STATUS_LOW; //低电平,下降沿
21 if( PWM_STATUS_SUCCESS==current_pwm.pwm_input_io_changed(¤t_pwm, flag, curr_cnt) ){ //判断是否完整获取过一个周期
22 pulse = ns_to_us(current_pwm.pulse); //获取占空比时间,单位us
23 period = ns_to_us(current_pwm.period); //获取周期,单位us29 }
30 }
31 }
32
33 //定时器中断调用覆盖,基于cubeMX开发
34 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
35 {
36 if(htim==&htim3){ //使用定时器三
37 current_pwm.pwm_input_timer_count(¤t_pwm); //更新定时器计数值
38 }
39 }
|